package com.woniuxy.hrp.service.Serviceimpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.hrp.entity.*;
import com.woniuxy.hrp.mapper.DoctorAndTimeMapper;
import com.woniuxy.hrp.mapper.DoctorAndTimeMapper;
import com.woniuxy.hrp.mapper.DoctorMapper;
import com.woniuxy.hrp.mapper.ReservationMapper;
import com.woniuxy.hrp.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
@Slf4j
public class DoctorServiceImp extends ServiceImpl<DoctorMapper,Doctor> implements DoctorService  {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private DoctorAndTimeMapper doctorAndTimeMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public int preAppoint(int id,String datetime) {
        /**
         * @Author 李国杰
         * @Description //TODO
         * @Date 22:10 2022/8/29
         * @Param [id]
         * @return int 输出时间id为成功 1为不在预约时间内 2为超过预约人数 3为特殊情况该医生不可预约
         **/
        for (DoctorAndTime r : doctorMapper.seleAllById(id).getDoctorAndTimes()) {
            //数据库格式装换
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            //判断是否在工作的时间段
            if (LocalDateTime.parse(datetime, format).compareTo(LocalDateTime.parse(r.getArrangeTime().getStartTime(), format)) > 0
                    && LocalDateTime.parse(datetime, format).compareTo(LocalDateTime.parse(r.getArrangeTime().getEndTime(), format)) < 0) {
                System.out.println("当前位于预约时间");
                System.out.println(doctorAndTimeMapper.seleById(r.getId()));
                //判断是否超过数量
                if (doctorAndTimeMapper.seleById(r.getId())!=null&&doctorAndTimeMapper.seleById(r.getId()).getReservations().size() >= r.getNum()) {
                    return -2;
                //判断医生状态
                } else if (!"y".equals(r.getStatus())) {
                    return -3;
                } else {
                    return r.getId();
                }
            }
        }
        return -1;
    }

    @Override
    public List<Doctor> seleByCon(Doctor doctor) {
        return doctorMapper.selectByCondition(doctor);
    }

    @Override
    public Doctor getArrangeTimeById(int id) {
        return doctorMapper.getPaiBanById(id);
    }

    /***
     * @description: 医生登录
     * @param: 帐号、密码
     * @return: com.woniuxy.hrp.entity.Doctor
     * @author Lrw
     * @date: 2022/8/31 10:49
     */
    @Override
    public Doctor login(String account, String password) {
        return doctorMapper.selectByAccAndPsw(account,password);
    }

    @Override
    public PageBean<Doctor> getByPage(int current,int limit) {
        PageBean<Doctor> pageBean= new PageBean<>();
        Page<Doctor> page = PageHelper.startPage(current, limit);
        List<Doctor> data =doctorMapper.selectAll();
        pageBean.setData(data);
        pageBean.setCurrPage(current);
        pageBean.setCurrPageSize(data.size());
        pageBean.setTotalNums((int) page.getTotal());
        pageBean.setTotalPages(page.getPages());
        pageBean.setPageSize(limit);
        return pageBean;
    }

    /**
     *@Description 医生功能：查询id医生个人信息
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public Doctor selectByDoctorId(int id) {
        return doctorMapper.selectByDoctorId(id);
    }

    /**
     *@Description 医生功能：修改个人密码
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void updatePassword(int id,String password) {
        doctorMapper.updatePassword(id,password);
    }

    /**
     *@Description 医生功能：修改预约状态为已完成
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void updateStatus(int id) {
        if (reservationMapper.selectByPatientId(id).getStatus().equals("n")){
            reservationMapper.updateByPatientId(id, "y");
        }else {
            reservationMapper.updateByPatientId(id, "n");
        }
    }

    /**
     *@Description 医生功能：查询出所有的预约信息(包含时间排序)
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public Doctor findByIdOrderByTime(int id) {

        return doctorMapper.findByIdOrderByTime(id);
    }


    /**
     *@Description 管理员功能：注册医生
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void add(Doctor doctor) {
        doctorMapper.add(doctor);
    }

    /**
     *@Description 管理员功能：（逻辑）删除医生
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void delete(int id) {
        doctorMapper.delete(id);
    }

    /**
     *@Description 管理员功能：根据id认证医生帐号（默认为未认证：n)
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void authById(int id) {
        doctorMapper.authById(id);
    }

    /**
     *@Description 管理员功能：动态修改指定id的医生信息(姓名、帐号、状态不能修改）
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void update(Doctor doctor) {
               doctorMapper.update(doctor);
    }

    /**
     *@Description 管理员功能：所有医生数据集合
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public List<Doctor> selectAll() {
        return doctorMapper.selectAll();
    }

    /**
     *@Description 管理员功能：根据条件查询医生信息
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public List<Doctor> selectByCondition(Doctor doctor) {
        return doctorMapper.selectByCondition(doctor);
    }
    @Override
    public Doctor getDPById(int id) {
        /**
         * @Author 李国杰
         * @Description //获取部门和职位
         * @Date 11:38 2022/9/3
         * @Param
         * @return
         **/
        return doctorMapper.seleDPById(id);
    }

    /**
     *根据医生id获取其病人信息
     * @param id
     * @return
     */
    @Override
    public Doctor getPatientByDoctorId(int id) {
        Doctor patientByDoctorId = doctorMapper.getPatientByDoctorId(id);
        return patientByDoctorId;
    }

    /**
     *@Description 根据患者id增加患者药品和数量
     *@Author TJ
     *@Version 1.0
     **/
    @Override
    public void addPatientMedicine(DoctorPatientMedicine doctorPatientMedicine) {


        doctorMapper.addPatientMedicine(doctorPatientMedicine);
    }

}
